package com.ericsson.epc.modules.backup.task;

import com.ericsson.common.quartz.task.ManualTask;
import com.ericsson.common.util.DateUtils;
import com.ericsson.common.util.FileUtil;
import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.enap.modules.autoCheck.entity.NetEops;
import com.ericsson.enap.modules.autoCheck.service.NetAutoCheckService;
import com.ericsson.enap.modules.backup.constant.BackupConstant;
import com.ericsson.epc.modules.backup.service.EpcDataBackupService;
import com.ericsson.epc.modules.ebmAutoCollector.service.EbmService;
import com.ericsson.epc.modules.sys.entity.EPCNetElement;
import com.ericsson.epc.modules.sys.service.EPCNetElementService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * @description: epc 网元主任务入口
 * @author: wangjingshi
 * @create: 2021-07-05 09:30
 */
@Component
public class EpcDataBackupTask implements ManualTask {

    private Logger log = LoggerUtils.Logger(LogFileName.EpcDataBackupJob);
    @Autowired
    private EPCNetElementService epcNetElementService;
    @Autowired
    private EpcDataBackupService epcDataBackupService;
    @Autowired
    private NetAutoCheckService netAutoCheckService;
    @Autowired
    private EbmService ebmService;


    @Override
    public void execute() {
        long startTime = new Date().getTime();
        log.info(" ==== startTime :"+startTime);
        String date = DateUtils.formatDate(new Date());//yyyy-MM-dd
        Map<String,Object> paramMap = ebmService.queryParamMap();
        //处理备份MME网元
        List<EPCNetElement> mmeList = epcNetElementService.getList(1L);
        log.info(" ==== do mme start ====");
        for (EPCNetElement mme:mmeList){
            epcDataBackupService.execMmeBackup(mme,date);
            epcDataBackupService.execMmeLog(mme,date,paramMap);
            cleanOldData(date,mme,paramMap);
        }
        log.info(" ==== do mme end ====");
        //处理备份SAGEW-C网元
        log.info(" ==== do saegw-c start ====");
        List<EPCNetElement> saegwCList = epcNetElementService.getList(2L);
        for (EPCNetElement saegwc:saegwCList){
            epcDataBackupService.execSaegwBackup(saegwc,date);
            epcDataBackupService.execSaegwLog(saegwc,date);
            cleanOldData(date,saegwc,paramMap);
        }
        log.info(" ==== do saegw-c end ====");
        //处理备份SAGEW-U网元
        log.info(" ==== do saegw-u start ====");
        List<EPCNetElement> saegwUList = epcNetElementService.getList(4L);
        //CountDownLatch latch = new CountDownLatch(saegwUList.size());
        //ExecutorService enipThreadPool = Executors.newFixedThreadPool(5);
        for (EPCNetElement saegwu:saegwUList){
//                ExecuteSaegwUTask task = new ExecuteSaegwUTask(epcDataBackupService,saegwu,date,latch);
//                enipThreadPool.execute(task);
                epcDataBackupService.execSaegwUBackup(saegwu,date);
                epcDataBackupService.execSaegwLog(saegwu,date);
                cleanOldData(date,saegwu,paramMap);
        }
//        try {
//            latch.await();
//            log.info("===== All saegw-u  end! =====");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        log.info(" ==== do saegw-u end ====");
        //处理备份pcrf网元
        log.info(" ==== do pcrf start ====");
        List<EPCNetElement> pcrfList = epcNetElementService.getList(3L);
        for (EPCNetElement pcrf:pcrfList){
            epcDataBackupService.execPcrfBackup(pcrf,date);
            epcDataBackupService.execPcrfLog(pcrf,date,paramMap);
            cleanOldData(date,pcrf,paramMap);
        }
        log.info(" ==== do pcrf end ====");
        //处理备份Cg
        log.info(" ==== do cg start ====");
        List<NetEops> cgList = netAutoCheckService.getNetEopsListAll().stream().filter(x->"60".equals(x.getFsubtype())).collect(Collectors.toList());
        for (NetEops cg:cgList){
            epcDataBackupService.execCgBackup(cg,date);
            epcDataBackupService.execCgLog(cg,date);
            cleanOldData(date,cg,paramMap);
        }
        log.info(" ==== do cg end ====");
        long endTime = new Date().getTime();
        log.info(" ==== total time :"+(endTime-startTime)/1000/(double)60.00);
    }

    private void cleanOldData(String date, EPCNetElement epcNet,Map<String,Object> paramMap) {
        String cleanDays = (String)((Map)paramMap.get("BACKUP_CLEAN_DAY")).get("value");
        String cleanDate = DateUtils.beforNumDay(new Date(),-Integer.parseInt(cleanDays));
        String cleanDir = BackupConstant.localPath + cleanDate + "/" +
                epcNet.getType() + "/" + epcNet.getFname();
        File cleanFile = new File(cleanDir);
        FileUtil.deleteDir(cleanFile);
        epcDataBackupService.deleteOldData(cleanDate);
    }

    private void cleanOldData(String date, NetEops cg,Map<String,Object> paramMap) {
        String cleanDays = (String)((Map)paramMap.get("BACKUP_CLEAN_DAY")).get("value");
        String cleanDate = DateUtils.beforNumDay(new Date(),-Integer.parseInt(cleanDays));
        String cleanDir = BackupConstant.localPath + cleanDate + "/" +
                cg.getFsubtype() + "/" + cg.getFname();
        File cleanFile = new File(cleanDir);
        FileUtil.deleteDir(cleanFile);
        epcDataBackupService.deleteOldData(cleanDate);
    }
}
